package com.yh;

import javax.servlet.http.HttpSession;

import com.yh.exception.CodeException;

import cn.hutool.core.util.StrUtil;

public class Const {
	/**
	 * 获取在会话中存放的验证码的属性名
	 */
	public static final String CODE_NAME = "_CODE_NAME";
	
	/**
	 * 验证码的服务器端验证
	 * @param session
	 * @param code
	 * @throws CodeException 10:验证码不能为空 11:验证码已失效 12:验证码不匹配
	 */
	public static void codeVerify(HttpSession session,String code)throws CodeException{
		if(StrUtil.isEmpty(code)){
			throw new CodeException(10, "验证码不能为空");
		}
		if(session.getAttribute(CODE_NAME)==null){
			throw new CodeException(11, "验证码已失效");
		}
		
		if(!code.equals(session.getAttribute(CODE_NAME))){
			throw new CodeException(12, "验证码不匹配");
		}
	}
}
